import {
  GraphQLObjectType,
  GraphQLString,
  GraphQLID,
  GraphQLList
} from 'graphql'

import mongoose from 'mongoose'
const Info = mongoose.model('Info')

// const parameterType = new GraphQLObjectType({
//   name: 'parameters',
//   fields: {
//     key: {
//       type: GraphQLString
//     },
//     value: {
//       type: GraphQLString
//     }
//   }
// })

const objType = new GraphQLObjectType({
  name: 'mete',
  fields: {
    createdAt: {
      type: GraphQLString
    },
    updatedAt: {
      type: GraphQLString
    }
  }
})

export let InfoType = new GraphQLObjectType({
  name: 'Info',
  fields: {
    _id: {
      type: GraphQLID
    },
    name: {
      type: GraphQLString
    },
    job: {
      type: GraphQLString
    },
    school: {
      type: GraphQLString
    },
    time: {
      type: GraphQLString
    },
    status: {
      type: GraphQLString
    },
    phone: {
      type: GraphQLString
    },
    email: {
      type: GraphQLString
    },
    url: {
      type: GraphQLString
    },
    originalUrl: {
      type: GraphQLString
    },
    section: {
      type: GraphQLString
    },
    meta: {
      type: objType
    }
  }
})

export const createInfo = {
  type: InfoType,
  args: {
    name: {
      name: 'name',
      type: GraphQLString
    },
    job: {
      name: 'job',
      type: GraphQLString
    },
    school: {
      name: 'school',
      type: GraphQLString
    },
    status: {
      name: 'status',
      type: GraphQLString
    },
    phone: {
      name: 'phone',
      type: GraphQLString
    },
    email: {
      name: 'email',
      type: GraphQLString
    },
    url: {
      name: 'url',
      type: GraphQLString
    },
    originalUrl: {
      name: 'originalUrl',
      type: GraphQLString
    },
    section: {
      type: GraphQLString
    }
  },
  async resolve(root, params) {
    const info = new Info(params)
    return await info.save()
  }
}

export const updateInfo = {
  type: InfoType,
  args: {
    id: {
      name: 'id',
      type: GraphQLString
    },
    name: {
      name: 'name',
      type: GraphQLString
    },
    job: {
      name: 'job',
      type: GraphQLString
    },
    time: {
      name: 'time',
      type: GraphQLString
    },
    school: {
      name: 'school',
      type: GraphQLString
    },
    status: {
      name: 'status',
      type: GraphQLString
    },
    phone: {
      name: 'phone',
      type: GraphQLString
    },
    email: {
      name: 'email',
      type: GraphQLString
    },
    url: {
      name: 'url',
      type: GraphQLString
    },
    originalUrl: {
      name: 'originalUrl',
      type: GraphQLString
    },
    section: {
      type: GraphQLString
    }
  },
  async resolve(root, params) {
    // 更新数据的条件查询
    var wherestr = { _id: params.id }

    // 执行更新数据
    delete params['id']

    return await Info.updateOne(wherestr, params)
  }
}

export const infos = {
  type: new GraphQLList(InfoType),
  args: {
    status: {
      name: 'status',
      type: new GraphQLList(GraphQLString)
    },
    section: {
      name: 'section',
      type: GraphQLString
    }
  },
  async resolve(root, params) {
    if (
      !params.status ||
      params.status.length === 0 ||
      params.status.length === 3
    ) {
      return Info.find({
        section: params.section
      }).exec()
    } else {
      return Info.find({
        section: params.section,
        status: params.status
      }).exec()
      // if (params.status.length === 1) {
      //   return Info.find({
      //     status: params.status[0]
      //   }).exec()
      // } else {
      //   const info = await Info.find({
      //     status: { $in: [params.status[0], params.status[1]] }
      //   }).exec()
      //   // const info1 = await Info.find({
      //   //   status: params.status[0]
      //   // }).exec()
      //   // const info2 = await Info.find({
      //   //   status: params.status[1]
      //   // }).exec()
      //   return info
      // }
    }
  }
}
